package states;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.svg.SVGMorph;

import entity.Player;
import entity.Pokemon;

import ui.core.Button;
import ui.listener.ChangeGameStateListener;
import utils.ResourceManager;
import utils.SaveManager;

public class IntroState extends BasicGameState{
	public static final int ID = 2;
	private static StateBasedGame game;
	private static GameContainer container;
	Button backButton;
	Button pikachu;
	Button charmander;
	Button squirtle;
	Button bulbasaur;
	Image background;
	SaveManager svManager;
	
	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		svManager = new SaveManager();
		IntroState.game = game;
		IntroState.container = container;
		background = ResourceManager.getImage("choosePokemonBackground");
		backButton = new Button(container, ResourceManager.getImage("backButtonNormalImage"), 215, 570);
		backButton.setMouseOverImage(ResourceManager.getImage("backButtonOverImage"));
		backButton.addListener(new ChangeGameStateListener(game, MainMenuState.ID));
		
		pikachu = new Button(container, ResourceManager.getImage("pikachuButtonNormalImage"), 100, 90);		
		pikachu.setMouseOverImage(ResourceManager.getImage("pikachuButtonOverImage"));
		pikachu.addListener(new ChangeGameStateListener(game, InGameState.ID));
		pikachu.addListener(new ComponentListener() {			
			@Override
			public void componentActivated(AbstractComponent source) {
//				SaveManager.resetGame();
				svManager.resetGameDB();
				Player player = new Player();
//				player.setPokemon(new Pokemon(25));
				player.getPokemonList().set(0, new Pokemon(25));
				player.getFirstPokemon().setCurrentHp(100);
//				SaveManager.saveGame(player);
				try {
					IntroState.game.getState(InGameState.ID).init(IntroState.container, IntroState.game);
				} catch (SlickException e) {
					e.printStackTrace();
				}
			}
		});
		charmander = new Button(container, ResourceManager.getImage("charmanderButtonNormalImage"), 380, 90);		
		charmander.setMouseOverImage(ResourceManager.getImage("charmanderButtonOverImage"));
		charmander.addListener(new ChangeGameStateListener(game, InGameState.ID));
		charmander.addListener(new ComponentListener() {			
			@Override
			public void componentActivated(AbstractComponent source) {
				svManager.resetGameDB();
				Player player = new Player();
				player.getPokemonList().set(0, new Pokemon(4));
				player.getFirstPokemon().setCurrentHp(100);
//				SaveManager.resetGame();
//				Player player = new Player();
//				player.setPokemon(new Pokemon(4));
//				SaveManager.saveGame(player);	
				try {
					IntroState.game.getState(InGameState.ID).init(IntroState.container, IntroState.game);
				} catch (SlickException e) {
					e.printStackTrace();
				}
			}
		});
		
		bulbasaur = new Button(container, ResourceManager.getImage("bulbasaurButtonNormalImage"), 100, 350);		
		bulbasaur.setMouseOverImage(ResourceManager.getImage("bulbasaurButtonOverImage"));
		bulbasaur.addListener(new ChangeGameStateListener(game, InGameState.ID));
		bulbasaur.addListener(new ComponentListener() {			
			@Override
			public void componentActivated(AbstractComponent source) {
//				SaveManager.resetGame();
//				Player player = new Player();
//				player.setPokemon(new Pokemon(1));
//				SaveManager.saveGame(player);
				svManager.resetGameDB();
				Player player = new Player();
				player.getPokemonList().set(0, new Pokemon(1));
				player.getFirstPokemon().setCurrentHp(100);
				try {
					IntroState.game.getState(InGameState.ID).init(IntroState.container, IntroState.game);
				} catch (SlickException e) {
					e.printStackTrace();
				}
			}
		});
		
		squirtle = new Button(container, ResourceManager.getImage("squirtleButtonNormalImage"), 380, 350);		
		squirtle.setMouseOverImage(ResourceManager.getImage("squirtleButtonOverImage"));
		squirtle.addListener(new ChangeGameStateListener(game, InGameState.ID));
		squirtle.addListener(new ComponentListener() {			
			@Override
			public void componentActivated(AbstractComponent source) {
//				SaveManager.resetGame();
//				Player player = new Player();
//				player.setPokemon(new Pokemon(7));
//				SaveManager.saveGame(player);
				svManager.resetGameDB();
				Player player = new Player();
				player.getPokemonList().set(0, new Pokemon(7));
				player.getFirstPokemon().setCurrentHp(100);
				try {
					IntroState.game.getState(InGameState.ID).init(IntroState.container, IntroState.game);
				} catch (SlickException e) {
					e.printStackTrace();
				}
			}
		});
		
		
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		background.draw(0,0,container.getWidth(), container.getHeight());
		pikachu.render(container, g);
		charmander.render(container, g);
		squirtle.render(container, g);
		bulbasaur.render(container, g);
		backButton.render(container, g);		
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		
	}

	@Override
	public int getID() {
		return ID;
	}

}
